﻿using Dapper;
using SIG.DAL.Dapper.Model;
using SIG.Infrastructure.Cache;
using SIG.Resources.Admin;
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SIG.DAL.Dapper.Repositories
{
    public class RoleRepository
    {
        private readonly ICacheService _cacheService;
        public RoleRepository()
        {
            _cacheService = new CacheService();
        }

        private DbConnection _connection;
        public IEnumerable<Role> GetRoleByUserId(Guid userId)
        {
            IEnumerable<Role> result;
            using (_connection = Utilities.GetOpenConnection())
            {               
                var sql = "SELECT  Roles.* FROM Roles INNER JOIN UserRoles ON Roles.Id = UserRoles.RoleId WHERE(UserRoles.UserId = @UserId)";
                result = _connection.Query<Role>(sql, new { userId });           
            }
            return result;  
        }

        public Role GetById(int id)
        {
            Role result;
            using (_connection = Utilities.GetOpenConnection())
            {
                result = _connection.Get<Role>(id);
            }
            return result;
        }
        public int? Create(Role model)
        {
            int? result;
            using (_connection = Utilities.GetOpenConnection())
            {
                result = _connection.Insert(model);
            }
            return result;
        }

        public int Update(Role model)
        {
            int result;
            using (_connection = Utilities.GetOpenConnection())
            {
                result = _connection.Update(model);
            }
            return result;
        }
        public int Delete(Role model)
        {
            int result;
            using (_connection = Utilities.GetOpenConnection())
            {
                result = _connection.Delete(model);
            }
            return result;
        }

        public int Delete(int id)
        {
            int result;
            using (_connection = Utilities.GetOpenConnection())
            {
                result = _connection.Delete<Role>(id);
            }
            return result;
        }

        public void SetRoleMenus(int RoleId, int[] menuId)
        {
            using (_connection = Utilities.GetOpenConnection())
            {
                //result = _connection.Insert(model);
                string deleteQuery = "DELETE FROM RoleMenus WHERE RoleId = @RoleId";
                _connection.Execute(deleteQuery, new { RoleId = RoleId });

                foreach (var item in menuId)
                {
                    string query = "INSERT INTO RoleMenus(RoleId,MenuId)VALUES(@RoleId,@MenuId)";
                    _connection.Execute(query, new { RoleId = RoleId, MenuId = item });
                }              
            }

            //Role vRole = _roleRepository.GetElementById(RoleId);
            //vRole.Menus.Clear();
            //vRole.Menus = _menuRepository.GetAllElements().Where(m => menuId.Contains(m.Id)).ToList();
            //this.Update(vRole);

            var key = $"{EntityNames.Menu}s";
            _cacheService.Invalidate(key); //取消缓存

        }


        public IEnumerable<Role> GetItems()
        {
            IEnumerable<Role> result;
            using (_connection = Utilities.GetOpenConnection())
            {
                result = _connection.GetList<Role>();
            }
            return result;
        }
    }
}
